<?php
	require_once('bootstrap.php');
	
	function collect_data() {
		$dbstat = new DbStat();
		
		global $db;
		echo '<br /><b>Share Pool:</b><br />';
		$query = $db->db_query("select sum(decode(name,'free memory',bytes))/sum(bytes)*100 from v\$sgastat where pool='shared pool'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'The percentage of free memory against total memory in shared pool: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['PCT_FREE_MEM'] = $value;
		}
		echo '<br />';
		
		
		$query = $db->db_query("select (sum(getmisses)/sum(gets))*100 from v\$rowcache");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Dictionary cache hit ratio: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['DIC_HR'] = $value;
		}
		echo '<br />';
		
		$query = $db->db_query("select sum(reloads)/sum(pins)*100 from v\$librarycache");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Proportion of reload: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['PCT_RELOADS'] = $value;
		}
		echo '<br />';
		
		echo '<br /><b>Buffer Cache:</b><br />';
		$query = $db->db_query("select (1-pr.value/(bg.value+cg.value))*100 from v\$sysstat pr, v\$sysstat bg, v\$sysstat cg where pr.name='physical reads' and bg.name='db block gets' and cg.name='consistent gets'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Buffer Cache Hit Ratio: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['BUFFER_HR'] = $value;
		}
		echo '<br />';
		
		echo '<br /><b>Redo Log Buffer:</b><br />';
		$query = $db->db_query("select (req.value/wrt.value)*100 from v\$sysstat req, v\$sysstat wrt where req.name='redo log space requests' and wrt.name= 'redo writes'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Redo Space Wait Ratio: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['REDO_WAIT_R'] = $value;
		}
		echo '<br />';
		
		$query = $db->db_query("select immediate_misses/(immediate_gets + immediate_misses)*100 from v\$latch where name='redo allocation'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Immediate Contention of redo allocation: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['IMM_ALLOC'] = $value;
		}
		echo '<br />';
		
		$query = $db->db_query("select immediate_misses/(immediate_gets + immediate_misses)*100 from v\$latch where name='redo copy'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Immediate Contention of redo copy: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['IMM_COPY'] = $value;
		}
		echo '<br />';
		
		$query = $db->db_query("select misses/(gets+misses)*100 from v\$latch where name='redo allocation'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Wait contention of redo allocation: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['WAIT_ALLOC'] = $value;
		}
		echo '<br />';
		
		$query = $db->db_query("select misses/(gets+misses)*100 from v\$latch where name='redo allocation'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Wait contention of redo copy: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['WAIT_COPY'] = $value;
		}
		echo '<br />';
		
		echo '<br /><b>Sorts in Memory:</b><br />';
		$query = $db->db_query("select mem.value/(mem.value+dsk.value)*100 from v\$sysstat mem, v\$sysstat dsk where mem.name='sorts (memory)' and dsk.name='sorts (disk)'");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Proportion of data sorts: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['PCT_SORTS'] = $value;
		}
		echo '<br />';
		
		echo '<br /><b>Rollback Segments:</b><br />';
		$query = $db->db_query("select sum(waits)/sum(gets)*100 from v\$rollstat");
		$db->execute($query);
		$row = oci_fetch_assoc($query);
		foreach($row as $key => $value) {
			echo 'Rollback segment: '.number_format($value, 2, '.', ' ').'%; ';
			$dbstat->para['ROLLBACK'] = $value;
		}
		echo '<br />';
		
		$dbstat->add();
	}
	
	collect_data();

?>
